from flask import Flask, render_template, request
import os
import signal
from subprocess import run as procrun
from threading import Thread
import shutil

app = Flask(__name__, static_folder='static', static_url_path='/static', template_folder='public')

@app.get('/')
def index():
    root=request.args.get('root', 'D:\\')
    def info(x):
        path = os.path.join(root, x)
        return dict(name=x, path=path, size=os.stat(path).st_size, is_dir=os.path.isdir(path))
    return render_template('index.html', items=map(info, os.listdir(root)))

def run_sub():
    cwd = os.curdir
    user_data_dir = os.path.realpath(os.path.join(cwd, 'u'))
    procrun([
        r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe",
        f'--user-data-dir={user_data_dir}',
        '--no-first-run',
        '--no-proxy-server',
        '--safe-mode',
        '--disable-extensions',
        '--disable-background-mode',
        '--disable-plugins',
        '--disable-plugins-discovery',
        '--disable-translate',
        '--disable-features=Translate',
        '--bwsi',
        '--disable-sync',
        '--disable-sync-preferences',
        '--disable-component-update',
        '--allow-insecure-localhost',
        '--app=http://127.0.0.1:8999'
    ], cwd=cwd)
    shutil.rmtree(user_data_dir)
    signal.raise_signal(signal.SIGTERM)

if __name__=='__main__':
    th = Thread(target=run_sub)
    th.start()
    app.run("127.0.0.1", 8999, debug=False)
    th.join()
